package edu.whu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.whu.bean.Supplier;
import edu.whu.dao.SupplierMapper;
import edu.whu.exception.ProductException;
import edu.whu.service.SupplierService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    SupplierMapper mapper;

    public SupplierServiceImpl(SupplierMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public int insert(Supplier su) {
        return mapper.insert(su);
    }

    @Override
    public int update(Supplier su) {
        return mapper.updateById(su);
    }

    @Override
    public int delete(int id) throws ProductException {
        try {
            return mapper.deleteById(id);
        }catch (Exception e){
            throw  new ProductException("删除的商品供应商不存在!");
        }
    }

    @Override
    public List<Supplier> selectAll() {
        return mapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Supplier selectById(int id) {
        return mapper.selectById(id);
    }

    @Override
    public List<Supplier> selectByName(String name) {
        LambdaQueryWrapper<Supplier> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Supplier::getName,name);
        return mapper.selectList(wrapper);
    }

    @Override
    public int deleteByProductId(int id) throws ProductException {
        try {
            LambdaQueryWrapper<Supplier> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Supplier::getProductId,id);
            return mapper.delete(wrapper);
        }catch (Exception e){
            throw new ProductException("删除的商品编号不存在");
        }
    }
}
